package book.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import book.dao.BaseDao;
import book.dao.TypeDao;
import book.util.Type;

public class TypeDaoImpl implements TypeDao{
	private Connection connection;
	private ResultSet rs;
	private PreparedStatement pstmt;
	public int insertfrom(Type type) {
	int num=0;
	try {
	connection=BaseDao.getConnection();
	String sql="insert into type( book_type, type_num) values(?,?)";
	
		pstmt=connection.prepareStatement(sql);
		pstmt.setString(1, type.getBook_type());
		pstmt.setInt(2, type.getType_num());
		num=pstmt.executeUpdate();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		BaseDao.closeAll(rs, pstmt, connection);
	}
	
		return num;
	}

	public int deletefrom(String Book_type) {
		int num=0;
			try {
		connection=BaseDao.getConnection();
		String sql="delete from type where Book_type=?";
	
			pstmt=connection.prepareStatement(sql);
			pstmt.setString(1, Book_type);
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		
		return num;
	}

	public int updateset(Type type) {
		int num=0;
		try {
		connection=BaseDao.getConnection();
		String sql="update type set type_num=? where type_book=?";
		pstmt=connection.prepareStatement(sql);
		pstmt.setInt(1, type.getType_num());
		
			pstmt.setString(2, type.getBook_type());
			num=pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		return num;
	}

	public List<Type> findTypeAll() {
		List<Type> telist=new ArrayList<Type>();
		try {
		connection=BaseDao.getConnection();
		String sql="select type_book,type_num from type";
		
			pstmt=connection.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Type a=new Type(rs.getString("type_book"),rs.getInt("type_num"));
				telist.add(a);
			}
					
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeAll(rs, pstmt, connection);
		}
		
		return telist;
	}

}
